package com.model.defaults.menu;

import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.core.hibernate.ExceptionHandler;
import com.core.hibernate.exceptionevent.ExceptionEvent;
import com.core.utils.GlobalConstants;
import com.model.MenuItem;
import com.model.UserContextName;


public class ModelDefaultMenuItems {

	public static void createMenuItems ( Session session,
	                                     Map <String, UserContextName> userContexts ) {

		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();

			MenuItem pojo = null;



			pojo = new MenuItem();
			pojo.setName("mainMenu.menuDashboard");

			pojo.setServletNameToExecute(GlobalConstants.SERVLET_DASHBOARD);
			pojo.setUserContextNameToExecute(userContexts.get(GlobalConstants.USERCONTEXT_DASHBOARD));
			pojo.getUserContextNameVisibleIn().add(userContexts.get(GlobalConstants.USERCONTEXT_DASHBOARD));
			session.save(pojo);



			pojo = new MenuItem();
			pojo.setName("mainMenu.menuProjects");

			pojo.setServletNameToExecute(GlobalConstants.SERVLET_PROJECTLIST);
			pojo.setUserContextNameToExecute(userContexts.get(GlobalConstants.USERCONTEXT_PROJECTSLIST));
			pojo.getUserContextNameVisibleIn().add(userContexts.get(GlobalConstants.USERCONTEXT_TASK));
			pojo.getUserContextNameVisibleIn().add(userContexts.get(GlobalConstants.USERCONTEXT_VERSION));
			pojo.getUserContextNameVisibleIn().add(userContexts.get(GlobalConstants.USERCONTEXT_PROJECT));
			pojo.getUserContextNameVisibleIn().add(userContexts.get(GlobalConstants.USERCONTEXT_REPORT));
			pojo.getUserContextNameVisibleIn().add(userContexts.get(GlobalConstants.USERCONTEXT_TEAMMEMBER));
			session.save(pojo);



			pojo = new MenuItem();
			pojo.setName("mainMenu.menuAdministration");

			pojo.setUserContextNameToExecute(userContexts.get(GlobalConstants.USERCONTEXT_ADMINISTRATION));
			pojo.setServletNameToExecute(GlobalConstants.SERVLET_ADMINISTRATION);
			pojo.getUserContextNameVisibleIn().add(userContexts.get(GlobalConstants.USERCONTEXT_ADMINISTRATION));
			pojo.getUserContextNameVisibleIn().add(userContexts.get(GlobalConstants.USERCONTEXT_USER));
			pojo.getUserContextNameVisibleIn().add(userContexts.get(GlobalConstants.USERCONTEXT_GROUP));
			pojo.getUserContextNameVisibleIn().add(userContexts.get(GlobalConstants.USERCONTEXT_ROLE));
			session.save(pojo);

			transaction.commit();

		} catch ( RuntimeException rte ) {
			ExceptionHandler.handleException(rte, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
			if ( transaction != null ) {
				if ( transaction.isActive() ) {
					try {
						transaction.rollback();
					} catch ( HibernateException hibernateException ) {
						ExceptionHandler.handleException(hibernateException, new ExceptionEvent(ExceptionEvent.POJOSAMPLES));
					}
				}
			}
		}

	}
}
